package com.zmj.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSON;
import com.zmj.anno.MyAjax;
import com.zmj.anno.RequestPath;
import com.zmj.entity.Goods;
import com.zmj.entity.User;
import com.zmj.service.IGoodsService;
import com.zmj.service.impl.GoodsServiceImpl;
import com.zmj.utils.CartMessage;
import com.zmj.utils.Message;

public class ShopCarController {
	IGoodsService goodsServiceImpl=new GoodsServiceImpl();
	
	
	
	
	@RequestPath("/addShopCar.shop")
	@MyAjax
	public String addShopCar(HttpServletRequest request) {
			Map<Integer, Goods> shopCar=(Map<Integer, Goods>) request.getSession().getAttribute("shopCar");
			if (shopCar==null) {
				shopCar=new HashMap<Integer, Goods>();
			}
			String id=request.getParameter("gid");
			int gid=Integer.parseInt(id);
			
			if (null==shopCar.get(gid)) {
				Goods goods=goodsServiceImpl.selOneGood(gid);
				shopCar.put(gid, goods);
				System.out.println(goods.toString());
			}else {
				Goods goods=shopCar.get(gid);
				goods.setBuynumber(goods.getBuynumber()+1);
			}
			
			
			float priceCount = 0;
			
			for (Goods goods: shopCar.values()) {
				int num=goods.getBuynumber();
				BigDecimal price = goods.getGprice();//单价
				float dis = goods.getGdiscount();//折扣

				BigDecimal bd1 = price.multiply(new BigDecimal(num));
				BigDecimal bd2 = bd1.multiply(new BigDecimal(dis));
				
				priceCount += bd2.floatValue();
			}
			
			request.getSession().setAttribute("shopCarCount", priceCount);
			request.getSession().setAttribute("shopCar", shopCar);
			
			Integer num=(Integer) request.getSession().getAttribute("shopCartNum");
			if (null==num) {
				num=1;
			}else {
				num++;
			}
			request.getSession().setAttribute("shopCartNum", num);
			Message m=new Message();
			m.setCode(0);
			m.setMes(num.toString());
			return JSON.toJSONString(m);
	}

	@RequestPath("/subShopCar.shop")
	@MyAjax
	public String subShopCar(HttpServletRequest request) {

		// 1:从session里面获取购物车
		Map<Integer, Goods> shopCar = (Map<Integer, Goods>) request.getSession().getAttribute("shopCar");

		// 2:从购物车获取需要减数量的商品;
		String id = request.getParameter("gid");
		int gid=Integer.parseInt(id);

		Goods ge = shopCar.get(gid);

		//不是对数量进行-1了;而是从购物车移除这个商品
		if(ge.getBuynumber() <= 1){
			shopCar.remove(gid);
		}else{
			ge.setBuynumber(ge.getBuynumber() - 1);
		}

		// 3: 减1以后返回结果
		float priceCount = 0;
		
		for (Goods goods: shopCar.values()) {
			int num=goods.getBuynumber();
			BigDecimal price = goods.getGprice();//单价
			float dis = goods.getGdiscount();//折扣

			BigDecimal bd1 = price.multiply(new BigDecimal(num));
			BigDecimal bd2 = bd1.multiply(new BigDecimal(dis));
			
			priceCount += bd2.floatValue();
		}
		
		request.getSession().setAttribute("shopCarCount", priceCount);

		// 需要将改好的购物车,保存到session里面去
		request.getSession().setAttribute("shopCar", shopCar);

		//从session里面获取总数量
		Integer number = (Integer) request.getSession().getAttribute("shopCartNum");
		number = number - 1;

		// 将我们购物车的总数量,存到会话里面
		request.getSession().setAttribute("shopCartNum", number);

		// 返回我们的响应结果;
		Message m=new Message();
		m.setCode(0);
		m.setMes(number.toString());
		return JSON.toJSONString(m);
	}
	
	@RequestPath("/delShopCar.shop")
	@MyAjax
	public String delShopCar(HttpServletRequest request) {
		Map<Integer, Goods> shopCar = (Map<Integer, Goods>) request.getSession().getAttribute("shopCar");
		String id=request.getParameter("gid");
		int gid=Integer.parseInt(id);
		shopCar.remove(gid);
		int count = 0;
		float priceCount = 0;
		int yf = 0;
		
		for (Goods goods: shopCar.values()) {
			count+=goods.getBuynumber();
			
			int num=goods.getBuynumber();
			BigDecimal price = goods.getGprice();//单价
			float dis = goods.getGdiscount();//折扣

			BigDecimal bd1 = price.multiply(new BigDecimal(num));
			BigDecimal bd2 = bd1.multiply(new BigDecimal(dis));
			
			priceCount += bd2.floatValue();
			yf++;
		}
		request.getSession().setAttribute("shopCartNum", count);
		request.getSession().setAttribute("shopCar", shopCar);
		request.getSession().setAttribute("shopCarCount", priceCount);
		CartMessage m=new CartMessage();
		m.setCode(1);
		m.setCount(count);
		m.setPriceCount(priceCount);
		m.setYf(yf);
		return JSON.toJSONString(m);
	}
}
